﻿using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Web.Mvc;

namespace Jquery.Validate.Additional
{
    /// <summary>
    /// 手机号码校验
    /// 正确的手机号码(如:13800571506 013800571505)则返回ture,否则返回false
    /// </summary>
    public class MobilePhoneAttribute : RegularExpressionAttribute, IClientValidatable
    {
        private const string DefaultPattern = @"^(13[0-9]|15[0|3|6|7|8|9]|18[8|9])\d{8}$";

        public MobilePhoneAttribute()
            : base(DefaultPattern)
        {
            ErrorMessage = "手机号格式错误";
        }

        public IEnumerable<ModelClientValidationRule> GetClientValidationRules(ModelMetadata metadata, ControllerContext context)
        {
            var rule = new ModelClientValidationRule
            {
                ErrorMessage = ErrorMessage,
                ValidationType = "mobilephone",
            };
            yield return rule;
        }
    }
}